﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoInsurance.Classes;
using System.Data.SqlClient;
namespace AutoInsurance
{
    public partial class Auth : System.Web.UI.Page
    {
        private Manager m1;
        private string Сon = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=AUTOINSURANCE;
Integrated Security=True;Pooling=False";
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Connection(string c)
        {
            try
            {
                SqlConnection connection = new SqlConnection(c);
                connection.Open();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Ошибка при установлении соединения: " + ex.Message);
            }
        }
        //кнопка для появления окна авторизации
        protected void Button3_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        //кнопка авторизации
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
            SqlConnection connection = new SqlConnection(Сon);
            connection.Open();
            
                List<Manager> userList = new List<Manager>();
                SqlCommand сommand2 = new SqlCommand("select * from Manager where Password='" + TextBox2.Text + "' and Login='" + TextBox1.Text + "'", connection);
                SqlDataReader reader = сommand2.ExecuteReader();

                while (reader.Read())
                {
                    userList.Add(new Manager(/*Convert.ToInt32(reader["id_manager"]),*/ Convert.ToString(reader["login"]), Convert.ToString(reader["surname"]), Convert.ToString(reader["name"]), Convert.ToString(reader["password"]), Convert.ToInt32(reader["Role"]), Convert.ToString(reader["Email"]), Convert.ToString(reader["Phone"])));
                }

                reader.Close();

                if (userList.Count() == 1)
                {
                    m1 = userList.First();
                    Session["login"] = m1.Login;
                    Session["name"] = m1.Name;
                    Session["surname"] = m1.Surname;
                    Session["role"] = m1.Role;
                    Button3.Visible = true;
                    Label2.Visible = false;
                    TextBox1.Visible = false;
                    TextBox2.Visible = false;
                    Label5.Visible = false;
                    Label6.Visible = false;
                    Button1.Visible = false;
                    HyperLink1.Visible = true;

                    Button2.Visible = true;
                    Label4.Visible = false;


                        
                    
                }
                    else
                {
                    Button3.Visible = false;
                    
                    Label4.Visible = true;
                }

                connection.Close();
            string myUrl = "Default.aspx?ID=" + m1.Role;
            Response.Redirect(myUrl);    
                     
                }
                
catch (Exception ex)      
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
                //string myUrl1 = "Error.aspx?error=" + ex.Message;
                //Response.Redirect(myUrl1);

            }

        }
        //кнопка выхода
        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["name"] = null;
            Session["surname"] = null;
            Session["role"] = null;

            Label5.Visible = true;
            Label6.Visible = true;
            Label2.Visible = true;
            TextBox1.Visible = true;
            TextBox1.Text = "";
            TextBox2.Visible = true;
            TextBox2.Text = "";
            Button1.Visible = true;
            Button3.Visible = false;
            Button2.Visible = false;


        }

    }
}